package com.cskaoyan.service;

import com.cskaoyan.bean.market.Aftersale;
import com.cskaoyan.bean.market.AftersaleExample;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.market.AftersaleListBean;
import com.cskaoyan.bean.market.AftersaleListVo;
import com.cskaoyan.mapper.AftersaleMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AftersaleServiceImpl implements AftersaleService {

    @Autowired
    AftersaleMapper aftersaleMapper;

    @Override
    public AftersaleListVo list(BaseParam baseParam, Integer orderId, String status) {
        PageUtil.start(baseParam);
        AftersaleExample aftersaleExample = new AftersaleExample();
        aftersaleExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        AftersaleExample.Criteria criteria = aftersaleExample.createCriteria();
        if(orderId != null && !"".equals(orderId)){
            criteria.andOrderIdEqualTo(orderId);
        }
        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }
        List<Aftersale> aftersales = aftersaleMapper.selectByExample(aftersaleExample);
        PageInfo<Aftersale> aftersalePageInfo = new PageInfo<>(aftersales);
        long total = aftersalePageInfo.getTotal();
        int pages = aftersalePageInfo.getPages();
        int page = aftersalePageInfo.getPageNum();
        Integer limit = baseParam.getLimit();
        ArrayList<AftersaleListBean> aftersaleListBeans = new ArrayList<>();

        for (Aftersale aftersale : aftersales) {
            AftersaleListBean aftersaleListBean = new AftersaleListBean(aftersale.getId(), aftersale.getAftersaleSn(), aftersale.getOrderId(), aftersale.getUserId(), aftersale.getType(), aftersale.getReason(), aftersale.getAmount(), aftersale.getComment(), aftersale.getStatus(), aftersale.getHandleTime(), aftersale.getAddTime(), aftersale.getUpdateTime(), aftersale.getDeleted(), aftersale.getPictures());
            aftersaleListBeans.add(aftersaleListBean);
        }
        AftersaleListVo aftersaleListVo = new AftersaleListVo();
        aftersaleListVo.setLimit(limit);
        aftersaleListVo.setList(aftersaleListBeans);
        aftersaleListVo.setPage(page);
        aftersaleListVo.setPages(pages);
        aftersaleListVo.setTotal((int) total);
        return aftersaleListVo;
    }

    @Override
    public void batchRecept(Map map) {
        //{"ids":[5]}
        List ids = (List) map.get("ids");
        for (Object id : ids) {
            Integer id1 = (Integer) id;
            Aftersale aftersale = new Aftersale();
            short status = 2;
            updateAfterSale(id1, aftersale, status);
        }
    }

    @Override
    public void batchReject(Map map) {
        List ids = (List) map.get("ids");
        for (Object id : ids) {
            Integer id1 = (Integer) id;
            Aftersale aftersale = new Aftersale();
            short status = 4;
            updateAfterSale(id1, aftersale, status);
        }
    }

    private void updateAfterSale(Integer id1, Aftersale aftersale, short status) {
        aftersale.setStatus(status);
        aftersale.setId(id1);
        Date time = new Date();
        aftersale.setHandleTime(time);
        aftersale.setUpdateTime(time);
        aftersaleMapper.updateByPrimaryKeySelective(aftersale);
    }
}
